package com.elf.zero.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;

/**
 * 系统UI工具
 * Created by Lidong on 2018/12/14.
 */
public class SystemUIUtils {

    public static void setStatusBar(Activity activity,boolean transparent, int color, boolean light) {

        int flag=0;

        //6.0及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            //设置是否是透明你状态栏
            if (transparent) {
                flag |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            }else{
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                activity.getWindow().setStatusBarColor(color);
            }

            //设置图标高亮
            if (MiUIUtils.setStatusBarDarkMode(activity, light)) {
                return;
            }
            if (FlymeUtils.setStatusBarLightMode(activity, light)) {
                return;
            }

            if (light) {
                flag = flag | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            activity.getWindow().getDecorView().setSystemUiVisibility(flag);

            return;
        }

        //5.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            if (transparent) {
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else {
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                activity.getWindow().setStatusBarColor(color);
            }
            //oppo状态栏
            OppoUtils.setStatusBarLightMode(activity, light);
            FlymeUtils.setStatusBarDarkIcon(activity, light);
        }

        //5.0以下不处理
    }

    public static void setNavigationBar(Activity activity,boolean transparent, int color, boolean light) {

        int flag = activity.getWindow().getDecorView().getSystemUiVisibility();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //6.0及以上
            if (transparent) {
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                flag = flag | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
                activity.getWindow().setNavigationBarColor(Color.TRANSPARENT);
            } else {
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                //颜色
                activity.getWindow().setNavigationBarColor(color);
            }

            //高亮
            if (light && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                flag = flag | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
            }

            activity.getWindow().getDecorView().setSystemUiVisibility(flag);
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0
            if(transparent){
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            }else{
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            }
            activity.getWindow().setNavigationBarColor(color);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    /**
     * 获取状态栏高度
     * @param context context
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
